#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from QQWeiboAPI.qqAPI import QQAPI
from QQWeiboAPI import oauth
import unittest
import simplejson as json


class StatusesTest(unittest.TestCase):
    """
    Test for the statuses operation
    Statuses/home_timeline
    Statuses/public_timeline
    Statuses/user_timeline
    Statuses/mentions_timeline
    Statuses/ht_timeline
    Statuses/broadcast_timeline
    Statuses/special_timeline
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 32)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)

    def testStatusesHomeTimeLine(self):
        """test for statuses/home_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.home_timeline_statuses(format='json', pageflag='0',\
                                             pagetime='0', reqnum='5')
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
            print info.id
        for user in res.data.user:
            print user.username
            print user.nickname
    
    def testStatusesPublicTimeLine(self):
        """test for statuses/public_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.public_timeline_statuses(format='json', pos='0', \
                                               reqnum='20')
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
        for user in res.data.user:
            print user.username
            print user.nickname
    
    def testStatusesUserTimeLine(self):
        """test for statuses/user_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.user_timeline_statuses(format='json', pageflag='0', \
                                               pagetime='0', reqnum='20', \
                                               lastid='0', name='ywq5134')
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
            print info.id
        for user in res.data.user:
            print user.username
            print user.nickname
    
    def testStatusesMentionsTimeLine(self):
        """test for statuses/mentions_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.mentions_timeline_statuses(format='json', \
                                                 pageflag='0', \
                                                 pagetime='0', \
                                                 reqnum='20', \
                                                 lastid='0')
        self.assertEqual(res.ret, 0)

    def testStatusesHtTimeLine(self):
        """test for statuses/ht_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.ht_timeline_statuses(format='json', \
                                           pageflag='0', \
                                           reqnum='20', \
                                           httext='欧冠')
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
        for user in res.data.user:
            print user.username
            print user.nickname
    
    def testStatusesBroadcastTimeLine(self):
        """test for statuses/broadcast_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.broadcast_timeline_statuses(format='json', \
                                                  pageflag='0', \
                                                  pagetime='0', \
                                                  reqnum='20', \
                                                  lastid='0')
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
            print info.id
        for user in res.data.user:
            print user.username
            print user.nickname
    
    def testStatusesSpecialTimeLine(self):
        """test for statuses/special_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.special_timeline_statuses(format='json', \
                                                pageflag='0', \
                                                pagetime='0', \
                                                reqnum='20') 
        self.assertEqual(res.ret, 0)
        for info in res.data.info:
            print info.text
            print info.id
        for user in res.data.user:
            print user.username
            print user.nickname


if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(StatusesTest('testStatusesHomeTimeLine'))
    unittest.TextTestRunner().run(fast)
